<?php

/**
 * Warlock is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 * @package    warlock
 * @author     David Cole <neophyte@sourcetutor.com>
 * @license    GNU/GPL v2
 */

using('Extensions.IComponent');

class Component_Admin_menu extends IComponent 
{
    /**#@+
     * Required Core Objects
     *
     * These are references to the core objects required by this
     * class.
     *
     * @access private
     * @var    object
     */
    var $_Module;
    var $_User;
    /**#@-*/

    /**
     * Menu Array
     *
     * An indexed nested array holding all the menu
     * items the user has access to.
     *
     * @var array
     */
    var $_Menu = array();

    function __construct()
    {
        parent::__construct();
        
        // Get references to required objects
        $this->_Module = &Vault::Get('Module');
        $this->_User   = &Vault::Get('User');

        /*
         * Iterate over all the available admin modules to build up
         * a menu structure to which the user has access.
         */

        $dh = opendir(W_ENV_PATH.'/Extensions/Admin/');

        while (($file = readdir($dh)) !== false)
        {            
            if (($file != '.') && ($file != '..') && ($file != 'warlock') && file_exists(W_ENV_PATH.'/Extensions/Admin/'.$file.'/module.php'))
            {            
                if ($this->_User->HasAccess($file.'_'.$file))
                {                               
                    $module  = &$this->_Module->Get('Admin', $file);
                    $actions = $module->GetActions();
    
                    $this->_Menu[] = array('name'        => $module->_Name,
                                           'title'       => $module->_Title,
                                           'description' => '',
                                           'url'         => W_ENV_URL.'/admin/module;'.$module->_Name.'/');
                                           
                    $id = (count($this->_Menu) - 1);
    
                    foreach ($actions as $action)
                        if ((count($action['action']) == 1) && ($this->_User->HasAccess($file.'_'.$action['action'][0])) )
                            $this->_Menu[$id]['submenu'][] = array('name'        => $action['action'][0],
                                                                   'title'       => $action['title'],
                                                                   'description' => $action['description'],
                                                                   'url'         => W_ENV_URL.'/admin/module;'.$module->_Name.'/action;'.$action['action'][0].'/');
                }
            }
        }

        closedir($dh);
    }

    function menu()
    {
        return $this->_Menu;
    }
    
    function quicklaunch()
    {
        return '<div class="quick-launch">
        <a href="#"><img src="Images/icons/16x16-designview.png" width="16" height="16" /><span>Design View</span></a>
        <a href="#"><img src="Images/icons/16x16-messages.png" width="16" height="16" /><span>(2) Messages</span></a>      
        <a href="/admin/?logout"><img src="Images/icons/16x16-logout.png" width="16" height="16" /><span>Logout</span></a>
      </div>';
    }
}

?>